package com.example.demovote.controller;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 判断用户登录
        if (request.getSession().getAttribute("loginedUser")==null) {
            // 保存用户再跳转 ....
            request.getSession().setAttribute("ServletPath",request.getServletPath());
            // 使用响应重定向跳转登录页
            response.sendRedirect("/login.html?1");
            return false;
        }
        return  true;
    }
}
